import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:like_button/like_button.dart';
import 'package:live_music/AppPage.dart';
import 'package:live_music/models/moment_detail_model.dart';
import 'package:live_music/pages/other/click_see_other.dart';
import 'package:live_music/pages/video/widget/video_share_widget.dart';
import 'package:live_music/services/userService.dart';
import 'package:live_music/utils/colors.dart';

class MomentTitleDetail extends StatefulWidget {
  final MomentDetailModel momentDetailData;
  final Function addFollowed;
  MomentTitleDetail({Key key, this.addFollowed, this.momentDetailData})
      : super(key: key);
  @override
  _MomentTitleDetailState createState() => _MomentTitleDetailState();
}

class _MomentTitleDetailState extends State<MomentTitleDetail> {
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              GestureDetector(
                child: Icon(
                  Icons.arrow_back_ios,
                  size: 20,
                  color: Colors.black,
                ),
                onTap: () {
                  if (Get.parameters['id'] != null) {
                    Get.offNamed("/app_page");
                  } else {
                    Get.back();
                  }
                },
              ),
              SizedBox(
                width: 5,
              ),
              InkWell(
                  child: Container(
                    width: 36,
                    height: 36,
                    child: CircleAvatar(
                      backgroundImage:
                          NetworkImage(widget.momentDetailData.userAvatarUrl),
                    ),
                  ),
                  onTap: () {
                    Get.to(
                        PageViewOther(userId: widget.momentDetailData.userId));
                  }),
              SizedBox(
                width: 5,
              ),
              Container(
                child: Text(
                  widget.momentDetailData.userName,
                  style: TextStyle(
                      fontSize: 16,
                      color: Colors.black,
                      fontWeight: FontWeight.w500),
                ),
              )
            ],
          ),
          Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                  height: 30,
                  width: !widget.momentDetailData.hasFollowed ? 60 : 70,
                  margin: EdgeInsets.only(left: 10),
                  alignment: Alignment.center,
                  child: ElevatedButton(
                      style: ButtonStyle(
                          elevation: MaterialStateProperty.all(0),
                          shape: MaterialStateProperty.all(
                              RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(20))),
                          backgroundColor: MaterialStateProperty.all(
                            !widget.momentDetailData.hasFollowed
                                ? Colors.redAccent
                                : Colors.white,
                          )),
                      onPressed: () {
                        onTapLikeButton(!widget.momentDetailData.hasFollowed);
                      },
                      child: Center(
                        child: Text(
                          !widget.momentDetailData.hasFollowed ? '关注' : "已关注",
                          style: TextStyle(
                              color: !widget.momentDetailData.hasFollowed
                                  ? Colors.white
                                  : Colors.grey,
                              fontSize: 12,
                              fontWeight: FontWeight.w500),
                        ),
                      ))),
              SizedBox(
                width: 5,
              ),
              InkWell(
                onTap: showBottomShare,
                child: Container(
                  width: 20,
                  margin: EdgeInsets.only(top: 3),
                  child: Icon(
                    Icons.launch,
                    color: Colors.grey[600],
                    size: 20,
                  ),
                ),
              )
            ],
          )
        ],
      ),
    );
  }

  //展示分享布局
  void showBottomShare() {
    showModalBottomSheet(
        context: context,
        isScrollControlled: true, //可滚动 解除showModalBottomSheet最大显示屏幕一半的限制
        shape: RoundedRectangleBorder(
          borderRadius: const BorderRadius.only(
            topLeft: Radius.circular(10),
            topRight: Radius.circular(10),
          ),
        ),
        backgroundColor: Colors.white,
        builder: (context) {
          return VideoShareWidget(momentDetailData: widget.momentDetailData);
        });
  }

  Future<bool> onTapLikeButton(bool isLiked) async {
    Map<String, dynamic> data = {
      "targetId": widget.momentDetailData.userId,
      "targetType": 'USER',
    };
    if (isLiked) {
      await UserService.follow(data);
    } else {
      await UserService.cancelFollow(data);
    }
    widget.addFollowed(isLiked);
    return isLiked;
  }
}
